/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "line.h"

Line::Line():
m_x1(0),
m_y1(0),
m_x2(0),
m_y2(0)
{
}

Line::Line(Line &line)
{
	m_x1 = line.m_x1;
	m_y1 = line.m_y1;
	m_x2 = line.m_x2;
	m_y2 = line.m_y2;
}

Line::~Line()
{
}

JukuObject* Line::MakeACopy()
{
	Line *line = new Line(*this);
	return line;
}

void Line::WriteXML(TiXmlNode* root)
{
    TiXmlElement *element = new TiXmlElement("Line");
    root->LinkEndChild(element);
    
	element->SetAttribute("x1", m_x1);
	element->SetAttribute("y1", m_y1);
	element->SetAttribute("x2", m_x2);
	element->SetAttribute("y2", m_y2);

	WriteBaseXML(element);
}

JukuObject* Line::ReadFromXMLElement(TiXmlElement* element)
{
    Line *line = new Line();
	
	element->Attribute("x1", &(line->m_x1));
	element->Attribute("y1", &(line->m_y1));
	element->Attribute("x2", &(line->m_x2));
	element->Attribute("y2", &(line->m_y2));

	line->ReadBaseXML(element);

    return line;
}


void Line::Draw(wxDC &dc, wxWindow *window, wxPoint origin)
{
	dc.DrawLine(m_x1+origin.x, m_y1+origin.y, m_x2+origin.x, m_y2+origin.y);
}

BoardRect Line::GetDrawArea()
{
	int tx1, tx2;
	if(m_x1 < m_x2){ tx1 = m_x1; tx2 = m_x2; }
	else           { tx1 = m_x2; tx2 = m_x1; }

	int ty1, ty2;
	if(m_y1 < m_y2){ ty1 = m_y1; ty2 = m_y2; }
	else           { ty1 = m_y2; ty2 = m_y1; }

	return BoardRect(tx1-LINE_MAX_LINE_WIDTH/2, ty1-LINE_MAX_LINE_WIDTH/2,
			tx2+LINE_MAX_LINE_WIDTH, ty2+LINE_MAX_LINE_WIDTH);
}

void Line::Move(int dx, int dy)
{
	m_x1 += dx;
	m_y1 += dy;
	m_x2 += dx;
	m_y2 += dy;
}

void Line::Rotate(TurnDirection dir, BoardPoint origin)
{
	int ox1 = m_x1 - origin.m_x;
	int oy1 = m_y1 - origin.m_y;
	int ox2 = m_x2 - origin.m_x;
	int oy2 = m_y2 - origin.m_y;
	int nox1, noy1, nox2, noy2;
	if(dir==CCW){
		nox1 = oy1;
		nox2 = oy2;
		noy1 = -ox1;
		noy2 = -ox2;
	}
	else{ //CW
		nox1 = -oy1;
		nox2 = -oy2;
		noy1 = ox1;
		noy2 = ox2;
	}
	m_x1 = origin.m_x + nox1;
	m_y1 = origin.m_y + noy1;
	m_x2 = origin.m_x + nox2;
	m_y2 = origin.m_y + noy2;
}



